﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PMPUtils.Resources;
using PMPUtils.Management;
using PMPUtils.Form;
using PMPUtils.Goals;

namespace PMP.Goals
{
    public partial class GoalManagement_Scoreboard : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FormUtil.Authenticate(Session, Response);

            if (!IsPostBack)
            {
                txtGoalID.Text = Request.Params["PARENT_ID"];

                String owner = GoalsOperator.ReadGoalOwner(txtGoalID.Text.Trim());
                txtAuthor.Text = ResourceOperator.ReadResourceName(owner);
            }

            BindData();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            String author = txtAuthor.Text.Trim();
            String type = ddlTypes.SelectedValue.Trim();
            String score = ddlScores.SelectedValue.Trim();
            String notes = txtNotes.Text.Trim();
            String goal_id = txtGoalID.Text.Trim();

            String reviewer = FormUtil.GetSessionUserName(Session);

            try
            {
                ScoreboardOperator.InsertScore(goal_id, author, type, score, reviewer, notes);

                // Send update
                String goalTitle = GoalsOperator.ReadGoalDesc(txtGoalID.Text.Trim());
                String subject = "Reviewer: " + reviewer + " Scored <" + score + "> on <" + type + "> of <" + goalTitle + ">.";

                String userName = FormUtil.GetSessionUserName(Session);
                ScoreboardOperator.SendUpdateMessage(txtGoalID.Text.Trim(), subject, userName, notes);
            }
            catch (Exception ex)
            {
                Messenger.Show(Response, "Error: " + ex.Message);
                return;
            }

            Messenger.Show(Response, "Added Successfully!");

            DataBind();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Close()
        {
            PopupForm changeExpectedDueDate = new PopupForm(Response);
            changeExpectedDueDate.Close();
        }

        private void BindData()
        {
            SqlDataSourceScoreboard.SelectCommand =
                "SELECT [score], [type], [notes], [reviewer], [assignee], [goal_id], [id] FROM [Scoreboard] where goal_id = " + txtGoalID.Text.Trim() + " ORDER BY [type]";
            SqlDataSourceScoreboard.DataBind();
        }

        protected void grvScoreboard_OnRowUpdated(object sender, GridViewUpdatedEventArgs e)
        {
            BindData();
        }

        protected void grvScoreboard_OnRowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            BindData();
        }

        protected void grvScoreboard_OnRowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            BindData();
        }

        protected void grvScoreboard_OnRowEditing(object sender, GridViewEditEventArgs e)
        {
            grvScoreboard.EditIndex = (int)e.NewEditIndex;

            BindData();
        }
    }
}
